package com.zyh.system.supermarket.dao.impl;

import com.zyh.system.supermarket.dao.UserDao;
import com.zyh.system.supermarket.domain.UserInfo;
import com.zyh.system.supermarket.domain.UserState;

import java.util.Set;
import java.util.TreeMap;

/**
 * @Author zyh
 * @Date 2024/8/3 14:39
 * @Description Dao 层
 */

public class UserDaoImpl implements UserDao {
    public static final TreeMap<String, UserInfo> USER_SET = new TreeMap<>();

    static {
        USER_SET.put("1001", new UserInfo("冯程程", "铂金卡", 2000, UserState.合法账户));
        USER_SET.put("1003", new UserInfo("孙悟空", "钻石卡", 5000, UserState.被禁账户));
        USER_SET.put("1002", new UserInfo("张巧巧", "黄金卡", 3000, UserState.合法账户));
    }

    /**
     * 查询所有的会员信息
     *
     * @param
     * @return
     */
    @Override
    public void selectAll(TreeMap<String, UserInfo> map) {
        System.out.println("用户ID\t账号\t卡类型\t积分\t卡片状态");
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            UserInfo value = map.get(key);
            System.out.println(key + "\t" + value.getUserName() + "\t" + value.getCardType() + "\t" + value.getScore() + "\t" + value.getCardType());
        }
    }

    /**
     * 删除指定id会员信息
     *
     * @param
     * @param
     * @return
     */
    @Override
    public boolean deleteUserInfoById(TreeMap<String, UserInfo> map, String delId) {
        map.remove(delId);
        return true;
    }

    /**
     * 根据id查询信息,返回查询到的对象
     *
     * @param map
     * @param id
     * @return
     */
    @Override
    public UserInfo selectById(TreeMap<String, UserInfo> map, String id) {
        UserInfo userInfo = null;
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            UserInfo value = map.get(key);
            if (key.equals(id)) {
                userInfo = value;
            }
        }
        return userInfo;
    }

    /**
     * 添加会员信息功能
     *
     * @param map
     * @param id
     * @param userInfo
     * @return
     */
    @Override
    public boolean addUserInfo(TreeMap<String, UserInfo> map, String id, UserInfo userInfo) {
        map.put(id, userInfo);
        return true;
    }

    /**
     * 根据输入的id修改对应的积分
     *
     * @param map
     * @param id
     * @return
     */
    @Override
    public boolean updateScore(TreeMap<String, UserInfo> map, String id, Integer newScore) {
        UserInfo userInfo = selectById(map, id);
        if (userInfo!=null){
            //进行修改积分操作
            userInfo.setScore(newScore);
            return true;
        }
        return false;
    }
}
